package cn.kwq.project_manger.service.impl;

import cn.kwq.project_manger.exception.UnLockWyuSystemException;
import cn.kwq.project_manger.pojo.QUser;
import cn.kwq.project_manger.pojo.User;
import cn.kwq.project_manger.pojo.dto.ResumeUserDto;
import cn.kwq.project_manger.repostitory.UserRepository;
import cn.kwq.project_manger.service.UserService;
import com.querydsl.jpa.impl.JPAQueryFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Optional;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    UserRepository userRepository;
    @Autowired
    JPAQueryFactory jpaQueryFactory;

    @Override
    @Transactional(rollbackForClassName = "Exception.class")
    public User bindEmail(String email,Long id) {
        QUser user = QUser.user;
        jpaQueryFactory.update(user)
                .set(user.email, email)
                .where(user.id.eq(id))
                //执行更新
                .execute();
        User fetch = jpaQueryFactory.selectFrom(user)
                .where(user.id.eq(id))
                .fetchOne();
        return fetch;
    }

    @Override
    @Transactional(rollbackForClassName = "Exception.class")
    public User updateMyNickName(String nickName,Long id) {
        QUser user = QUser.user;
        jpaQueryFactory.update(user)
                .set(user.nickName, nickName)
                .where(user.id.eq(id))
                //执行更新
                .execute();
        User fetch = jpaQueryFactory.selectFrom(user)
                .where(user.id.eq(id))
                .fetchOne();
        return fetch;
    }

    //为简历获取学校相关信息
    @Override
    public ResumeUserDto getMsgForResume(Long id) {
        QUser user = QUser.user;
        User msgUser = jpaQueryFactory.selectFrom(user)
                .where(user.id.eq(id))
                .fetchOne();
        ResumeUserDto msg=ResumeUserDto
                .builder()
                .userName(msgUser.getUserName())
                .institute(msgUser.getInstitute())
                .major(msgUser.getMajor())
                .build();
        return msg;
    }



    @Override
    public User getUserById(Long id) {
        return userRepository.findById(id).get();
    }

    @Override
    @Transactional(rollbackForClassName = "Exception.class")
    public User unLockWyu(long loginId) throws UnLockWyuSystemException {
        try{
            Optional<User> one = userRepository.findById(loginId);
            User user = one.get();
            //将wyu的信息设为空
            user.setUserName(null);
            user.setStuNum(null);
            user.setInstitute(null);
            user.setGrade(null);
            user.setWYUclass(null);
            user.setMajor(null);

            return userRepository.save(user);
        }catch (Exception e){
            throw new UnLockWyuSystemException();
        }

    }


}
